home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / QuickDraw3D 1.6 SDK / Mac SampleCode Previous / Geometry Samples- Mac / BoxPaint+ / sources / BoxPaint_Support.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  11.1 KB  |  399 lines  |  [TEXT/CWIE]

  1. /*  BoxPaint_Support.c
  2.                                                                             
  3.     Quickdraw 3D sample code
  4.     
  5.     This file contains utility routines for QuickDraw 3d sample code. This
  6.     app shows how to apply a texture shader to an object.  Bear in mind
  7.     that any object that you wish to texture map needs to have UV parameters
  8.     applied.
  9.                                                                             
  10.     Nick Thompson
  11.     (c)1994-96 Apple computer Inc., All Rights Reserved                                
  12.  
  13. */
  14.  
  15. /* --------------------------------------------------------------------
  16. ** Includes
  17. */
  18.  
  19. #include    "BoxPaint_Support.h"
  20. #include    "BoxPaint_documentStructure.h"
  21.  
  22. #include    "QD3DDrawContext.h"
  23. #include    "QD3DRenderer.h"
  24. #include    "QD3DShader.h"
  25. #include    "QD3DCamera.h"
  26. #include    "QD3DLight.h"
  27. #include    "QD3DGeometry.h"
  28. #include    "QD3DGroup.h"
  29. #include    "QD3DMath.h"
  30. #include    "QD3DTransform.h"
  31.  
  32.  
  33. /*    #define        BOX
  34. */
  35.  
  36. /* --------------------------------------------------------------------
  37. ** Global Variables
  38. */
  39. static     TQ3Point3D    documentGroupCenter;
  40. static    float        documentGroupScale;
  41.  
  42. /* --------------------------------------------------------------------
  43. ** Local Functions
  44. */
  45. TQ3DrawContextObject    MyNewDrawContext(WindowPtr theWindow) ;
  46. TQ3CameraObject         MyNewCamera(WindowPtr theWindow) ;
  47. TQ3GroupObject            MyNewLights(void) ;
  48.  
  49.  
  50. TQ3ViewObject MyNewView(WindowPtr theWindow)
  51. {
  52.     TQ3Status                myStatus;
  53.     TQ3ViewObject            myView;
  54.     TQ3DrawContextObject    myDrawContext;
  55.     TQ3RendererObject        myRenderer;
  56.     TQ3CameraObject            myCamera;
  57.     TQ3GroupObject            myLights;
  58.     
  59.     myView = Q3View_New();
  60.     
  61.     /*     Create and set draw context. */
  62.     if ((myDrawContext = MyNewDrawContext(theWindow)) == nil )
  63.         goto bail;
  64.         
  65.     if ((myStatus = Q3View_SetDrawContext(myView, myDrawContext)) == kQ3Failure )
  66.         goto bail;
  67.  
  68.     Q3Object_Dispose( myDrawContext ) ;
  69.     
  70.     /*     Create and set renderer. */
  71.     
  72.     
  73.     
  74.     /*  this would use the wireframe renderer */
  75.     /*    Not very useful for this app    */
  76. #if 0
  77.     myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeWireFrame);
  78.     if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) == kQ3Failure ) {
  79.         goto bail;
  80.     }
  81. #else
  82.     /*  this would use the interactive plug-in renderer */
  83.  
  84.     if ((myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive)) != nil ) {
  85.         if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) == kQ3Failure ) {
  86.             goto bail;
  87.         }
  88.     }
  89.     else {
  90.         goto bail;
  91.     }
  92. #endif
  93.  
  94.     Q3Object_Dispose( myRenderer ) ;
  95.     
  96.     /*     Create and set camera. */
  97.     if ( (myCamera = MyNewCamera(theWindow)) == nil )
  98.         goto bail;
  99.         
  100.     if ((myStatus = Q3View_SetCamera(myView, myCamera)) == kQ3Failure )
  101.         goto bail;
  102.  
  103.     Q3Object_Dispose( myCamera ) ;
  104.     
  105.     /*     Create and set lights. */
  106.     if ((myLights = MyNewLights()) == nil )
  107.         goto bail;
  108.         
  109.     if ((myStatus = Q3View_SetLightGroup(myView, myLights)) == kQ3Failure )
  110.         goto bail;
  111.         
  112.     Q3Object_Dispose(myLights);
  113.  
  114.     /*     Done!!! */
  115.     return ( myView );
  116.     
  117. bail:
  118.     /*     If any of the above failed, then don't return a view. */
  119.     return ( nil );
  120. }
  121.  
  122. /* ---------------------------------------------------------------------------------- */
  123.  
  124. TQ3DrawContextObject MyNewDrawContext(WindowPtr theWindow)
  125. {
  126.     TQ3DrawContextData        myDrawContextData;
  127.     TQ3MacDrawContextData    myMacDrawContextData;
  128.     TQ3ColorARGB            ClearColor;
  129.     TQ3DrawContextObject    myDrawContext ;
  130.     
  131.     /*     Set the background color. */
  132.     ClearColor.a = 1.0;
  133.     ClearColor.r = 0.0;
  134.     ClearColor.g = 0.2;
  135.     ClearColor.b = 1.0;
  136.     
  137.     /*     Fill in draw context data. */
  138.     myDrawContextData.clearImageMethod = kQ3ClearMethodWithColor;
  139.     myDrawContextData.clearImageColor = ClearColor;
  140.     myDrawContextData.paneState = kQ3False;
  141.     myDrawContextData.maskState = kQ3False;
  142.     myDrawContextData.doubleBufferState = kQ3True;
  143.  
  144.     myMacDrawContextData.drawContextData = myDrawContextData;
  145.     
  146.     myMacDrawContextData.window = (CGrafPtr) theWindow;        /*  this is the window associated with the view */
  147.     myMacDrawContextData.library = kQ3Mac2DLibraryNone;
  148.     myMacDrawContextData.viewPort = nil;
  149.     myMacDrawContextData.grafPort = nil;
  150.     
  151.     /*     Create draw context and return it, if it’s nil the caller must handle */
  152.     myDrawContext = Q3MacDrawContext_New(&myMacDrawContextData) ;
  153.  
  154.     return myDrawContext ;
  155. }
  156.  
  157. /* ---------------------------------------------------------------------------------- */
  158.  
  159. TQ3CameraObject MyNewCamera(WindowPtr theWindow)
  160. {
  161.     TQ3ViewAngleAspectCameraData    perspectiveData;
  162.  
  163.     TQ3CameraData                    someCameraData;
  164.     
  165.     TQ3CameraObject                camera;
  166.     
  167.     TQ3Point3D                     from     = { 0.0, 0.0, 3.0 };
  168.     TQ3Point3D                     to         = { 0.0, 0.0, 0.0 };
  169.     TQ3Vector3D                 up         = { 0.0, 1.0, 0.0 };
  170.  
  171.     float                         fieldOfView = 1.0;
  172.     float                         hither         =  0.00001;
  173.     float                         yon         =  10;
  174.     
  175.     TQ3Status                    returnVal = kQ3Failure ;
  176.  
  177.  
  178.     someCameraData.placement.cameraLocation     = from;
  179.     someCameraData.placement.pointOfInterest     = to;
  180.     someCameraData.placement.upVector             = up;
  181.  
  182.     someCameraData.range.hither    = hither;
  183.     someCameraData.range.yon     = yon;
  184.  
  185.     someCameraData.viewPort.origin.x =  -1.0;
  186.     someCameraData.viewPort.origin.y = 1.0;
  187.     someCameraData.viewPort.width = 2.0;
  188.     someCameraData.viewPort.height = 2.0;
  189.     
  190.     perspectiveData.cameraData        = someCameraData;
  191.     perspectiveData.fov                = fieldOfView;
  192.     perspectiveData.aspectRatioXToY    =
  193.         (float) (theWindow->portRect.right - theWindow->portRect.left) / 
  194.         (float) (theWindow->portRect.bottom - theWindow->portRect.top);
  195.         
  196.     camera = Q3ViewAngleAspectCamera_New(&perspectiveData);
  197.     return camera ;
  198. }
  199.  
  200.  
  201. /* ---------------------------------------------------------------------------------- */
  202.  
  203. TQ3GroupObject MyNewLights(void)
  204. {
  205.     TQ3GroupPosition        myGroupPosition;
  206.     TQ3GroupObject            myLightList;
  207.     TQ3LightData            myLightData;
  208.     TQ3PointLightData        myPointLightData;
  209.     TQ3DirectionalLightData    myDirectionalLightData;
  210.     TQ3LightObject            myAmbientLight, myPointLight, myFillLight;
  211.     TQ3Point3D                pointLocation = { -10.0, 0.0, 10.0 };
  212.     TQ3Vector3D                fillDirection = { 10.0, 0.0, 10.0 };
  213.     TQ3ColorRGB                WhiteLight = { 1.0, 1.0, 1.0 };
  214.     
  215.     /*     Set up light data for ambient light.  This light data will be used for point and fill */
  216.     /*     light also. */
  217.  
  218.     myLightData.isOn = kQ3True;
  219.     myLightData.color = WhiteLight;
  220.     
  221.     /*     Create ambient light. */
  222.     myLightData.brightness = .3;
  223.     myAmbientLight = Q3AmbientLight_New(&myLightData);
  224.     if ( myAmbientLight == nil )
  225.         goto bail;
  226.     
  227.     /*     Create point light. */
  228.     myLightData.brightness = 0.60;
  229.     myPointLightData.lightData = myLightData;
  230.     myPointLightData.castsShadows = kQ3False;
  231.     myPointLightData.attenuation = kQ3AttenuationTypeNone;
  232.     myPointLightData.location = pointLocation;
  233.     myPointLight = Q3PointLight_New(&myPointLightData);
  234.     if ( myPointLight == nil )
  235.         goto bail;
  236.  
  237.     /*     Create fill light. */
  238.     myLightData.brightness = .2;
  239.     myDirectionalLightData.lightData = myLightData;
  240.     myDirectionalLightData.castsShadows = kQ3False;
  241.     myDirectionalLightData.direction = fillDirection;
  242.     myFillLight = Q3DirectionalLight_New(&myDirectionalLightData);
  243.     if ( myFillLight == nil )
  244.         goto bail;
  245.  
  246.     /*     Create light group and add each of the lights into the group. */
  247.     myLightList = Q3LightGroup_New();
  248.     if ( myLightList == nil )
  249.         goto bail;
  250.     myGroupPosition = Q3Group_AddObject(myLightList, myAmbientLight);
  251.     if ( myGroupPosition == 0 )
  252.         goto bail;
  253.     myGroupPosition = Q3Group_AddObject(myLightList, myPointLight);
  254.     if ( myGroupPosition == 0 )
  255.         goto bail;
  256.     myGroupPosition = Q3Group_AddObject(myLightList, myFillLight);
  257.     if ( myGroupPosition == 0 )
  258.         goto bail;
  259.  
  260.     Q3Object_Dispose( myAmbientLight ) ;
  261.     Q3Object_Dispose( myPointLight ) ;
  262.     Q3Object_Dispose( myFillLight ) ;
  263.  
  264.     /*     Done! */
  265.     return ( myLightList );
  266.     
  267. bail:
  268.     /*     If any of the above failed, then return nothing! */
  269.     return ( nil );
  270. }
  271.  
  272.  
  273. static TQ3GroupPosition MyAddTransformedObjectToGroup( TQ3GroupObject theGroup, TQ3Object theObject, TQ3Vector3D *translation )
  274. {
  275.     TQ3TransformObject    transform;
  276.  
  277.     transform = Q3TranslateTransform_New(translation);
  278.     Q3Group_AddObject(theGroup, transform);    
  279.     Q3Object_Dispose(transform);
  280.     return Q3Group_AddObject(theGroup, theObject);    
  281. }
  282.  
  283. TQ3GroupObject MyNewModel()
  284. {
  285.     TQ3GroupObject            myGroup = NULL;
  286.     TQ3GeometryObject        myBox = NULL,  myEllipsoid = NULL;
  287.     TQ3ShaderObject            myIlluminationShader = NULL;
  288.     TQ3StyleObject            theStyleObject = NULL;
  289.     
  290.     TQ3BoxData                myBoxData;
  291.     TQ3EllipsoidData        myEllipsoidData;
  292.     TQ3Vector3D                translation;
  293.     TQ3DisplayGroupState    theState;
  294.     TQ3SubdivisionStyleData    theSubdivisionStyleData;
  295.     
  296.     if ((myGroup = Q3DisplayGroup_New()) != NULL ) {
  297.         
  298.         
  299.         /*  Define a shading type for the group */
  300.         /*  and add the shader to the group */
  301.     
  302. /*        myIlluminationShader = Q3PhongIllumination_New();
  303. */        myIlluminationShader = Q3LambertIllumination_New();
  304.         Q3Group_AddObject(myGroup, myIlluminationShader);
  305.         
  306. /*        theStyleObject = Q3InterpolationStyle_New(kQ3InterpolationStyleVertex);
  307.         Q3Group_AddObject(myGroup, theStyleObject) ;
  308.         Q3Object_Dispose(theStyleObject);
  309. */        
  310.         theSubdivisionStyleData.method = kQ3SubdivisionMethodConstant;
  311.         theSubdivisionStyleData.c1 = 16;
  312.         theSubdivisionStyleData.c2 = 16;
  313.         
  314.         theStyleObject = Q3SubdivisionStyle_New(&theSubdivisionStyleData);
  315.         Q3Group_AddObject(myGroup, theStyleObject) ;
  316.         Q3Object_Dispose(theStyleObject);
  317.     
  318.  
  319. #ifdef    BOX
  320.         myBoxData.boxAttributeSet = nil;
  321.         myBoxData.faceAttributeSet = nil;
  322.         
  323.         /*  create the box itself */
  324.         Q3Point3D_Set(&myBoxData.origin, 0, 0, 0);
  325.         Q3Vector3D_Set(&myBoxData.orientation, 0, 1, 0);
  326.         Q3Vector3D_Set(&myBoxData.majorAxis, 0, 0, 1);    
  327.         Q3Vector3D_Set(&myBoxData.minorAxis, 1, 0, 0);    
  328.         myBox = Q3Box_New(&myBoxData);
  329.         
  330.         /*  put four copies of the box into the group, each one with its own translation */
  331.         translation.x = -0.5;translation.y = -0.5;translation.z = -0.5;
  332.         MyAddTransformedObjectToGroup( myGroup, myBox, &translation ) ;
  333. #else
  334.         
  335.         translation;    /*    no op because it's not used here    */
  336.         myBoxData;        /*    no op because it's not used here    */
  337.         myBox;            /*    no op because it's not used here    */
  338.         myEllipsoidData.interiorAttributeSet = NULL;
  339.         myEllipsoidData.ellipsoidAttributeSet = NULL;
  340.         myEllipsoidData.caps = kQ3EndCapNone;
  341.         
  342.         Q3Point3D_Set(&myEllipsoidData.origin, 0.0, 0.0, 0.0);
  343.         Q3Vector3D_Set(&myEllipsoidData.orientation, 0.0, 1.3, 0.0);
  344.         Q3Vector3D_Set(&myEllipsoidData.minorRadius, -1.0, 0.0, 0.0);
  345.         Q3Vector3D_Set(&myEllipsoidData.majorRadius, 0.0, 0.0, -1.0);
  346.         myEllipsoidData.uMin = 0.0; myEllipsoidData.uMax = 1.0;
  347.         myEllipsoidData.vMin = 0.0; myEllipsoidData.vMax = 1.0;
  348.  
  349.         myEllipsoid = Q3Ellipsoid_New(&myEllipsoidData);
  350.         Q3Group_AddObject(myGroup, myEllipsoid);
  351. #endif
  352.     }
  353.  
  354.     
  355.     /*  dispose of the objects we created here */
  356.     if( myIlluminationShader ) 
  357.         Q3Object_Dispose(myIlluminationShader);    
  358.                 
  359. #ifdef    BOX
  360.     if( myBox ) 
  361.         Q3Object_Dispose( myBox );
  362. #else    
  363.     if( myEllipsoid ) 
  364.         Q3Object_Dispose( myEllipsoid );
  365. #endif    
  366.  
  367.         /*    Now, get the state of the current model    */
  368.         
  369.         Q3DisplayGroup_GetState(myGroup, &theState);
  370.         
  371.         /*    we don't want to push and pop for PERFORMANCE!!    */
  372.             
  373.         theState = theState | (kQ3DisplayGroupStateMaskIsInline);
  374.         Q3DisplayGroup_SetState(myGroup,  theState);
  375.  
  376. /*     Done! */
  377.     return ( myGroup );
  378. }
  379.  
  380.  
  381.  
  382. /* ----------------------------------------------------------------------------------------------- */
  383.  
  384. TQ3Status GetDocumentGroupBoundingBox( 
  385.     DocumentPtr theDocument , 
  386.     TQ3BoundingBox *viewBBox)
  387. {
  388.     TQ3Status        status;
  389.     TQ3ViewStatus    viewStatus ;
  390.     
  391.     status = Q3View_StartBoundingBox( theDocument->fView, kQ3ComputeBoundsExact );
  392.     do {
  393.         status = Document_SubmitScene( theDocument ) ;
  394.     } while((viewStatus = Q3View_EndBoundingBox( theDocument->fView, viewBBox )) == kQ3ViewStatusRetraverse );
  395.     return status ;
  396. }
  397.  
  398.  
  399.